package resp

import (
	"fmt"
)

/**
 * 分页参数
 */
type PageFields struct {
	DefinitivePageSize int `json:"page_size"` // 应使用Size()方法获取分页大小的实际值
	DefinitivePageNum  int `json:"page_num"`  // 应使用Num()方法获取分页页码的实际值
	PageCount          int `json:"page_count"`
	ItemCount          int `json:"item_count"`
}

func (d *PageFields) Num() int {
	if d.DefinitivePageNum == 0 {
		return 1
	}
	return d.DefinitivePageNum
}

func (d *PageFields) Size() int {
	if d.DefinitivePageSize == 0 {
		return 40
	}
	return d.DefinitivePageSize
}

func (d *PageFields) Offset() int {
	return d.Size() * (d.Num() - 1)
}

func (d *PageFields) Limit() string {
	return fmt.Sprintf("LIMIT %d, %d", d.Offset(), d.Size())
}

func (d *PageFields) SetItemCount(c int) *PageFields {
	if c > 0 {
		d.ItemCount = c
		d.PageCount = (c - 1 ) / d.Size() + 1
	}
	return d
}